
#if 1

#include "threadpool.h"
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
int done1; 
int done2;
void func1(void *arg) {
    printf("Function: %s\n", __func__);
    done1 = 0;
    int i = 0;
    for (i = 0; i < 3; i++) {
        printf("%lu: %d\n", pthread_self(), i + 10);
        usleep(100000);
    }
    done1 = 1;
}

void func2(void *arg) {
    printf("Function: %s\n", __func__);
    done2 = 0;
    int i = 0;
    for (i = 0; i < 3; i++) {
        printf("%lu: %d\n", pthread_self(), i);
        usleep(100000);
    }
    done2 = 1;
}

void *producer(void *arg) {
    printf("Function: %s\n", __func__);
    threadpool_t *pool = (threadpool_t *)arg;
    threadpool_commit(pool, func1, NULL);
    threadpool_commit(pool, func2, NULL);
    return NULL;
}

int main() {
    printf("Hello world\n");

    threadpool_t *pool = threadpool_create(4);
#if 1
    printf("change\n");
    for (int i = 0; i < 1; i++) {
        pthread_t prodect_th;
        pthread_create(&prodect_th, NULL, producer, pool);
        pthread_detach(prodect_th);
    }
#else
    threadpool_commit(pool, func1, NULL);
    threadpool_commit(pool, func2, NULL);
#endif

    while (!done1 && !done2) {
        printf("done1: %d, done2: %d\n", done1, done2);
        usleep(100000);
    }

    threadpool_terminate(pool);
    threadpool_waitdone(pool);

    return 0;
}
#elif

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "thrd_pool.h"

/**
 * shell: g++ taskqueue_test.cc -o taskqueue_test -lgtest -lgtest_main -lpthread
 */
#if 0
int done = 0;

pthread_mutex_t lock;

void do_task(void *arg) {
    thrdpool_t *pool = (thrdpool_t *)arg;
    pthread_mutex_lock(&lock);
    done++;
    pthread_t threadId = pthread_self();
    printf("doing = %d task. threadId = %lu \n", done,  (unsigned long)threadId);
    usleep(1000); // 休眠3毫秒
    pthread_mutex_unlock(&lock);
    usleep(1000); // 休眠3毫秒
    if (done >= 100) {
        thrdpool_terminate(pool);
    }
}

void test_thrdpool_basic() {
    int threads = 8;
    pthread_mutex_init(&lock, NULL);
    thrdpool_t *pool = thrdpool_create(threads);
    if (pool == NULL) {
        perror("thread pool create error!\n");
        exit(-1);
    }

    while (thrdpool_post(pool, &do_task, pool) == 0) {

    }

    thrdpool_waitdone(pool);
    pthread_mutex_destroy(&lock);
}

int main(int argc, char **argv) {
    test_thrdpool_basic();
    return 0;
}
#else
int done1; 
int done2;
void func1(void *arg) {
    printf("Function: %s\n", __func__);
    done1 = 0;
    int i = 0;
    for (i = 0; i < 3; i++) {
        printf("%lu: %d\n", pthread_self(), i + 10);
        usleep(100000);
    }
    done1 = 1;
}

void func2(void *arg) {
    printf("Function: %s\n", __func__);
    done2 = 0;
    int i = 0;
    for (i = 0; i < 3; i++) {
        printf("%lu: %d\n", pthread_self(), i);
        usleep(100000);
    }
    done2 = 1;
}

void *producer(void *arg) {
    printf("Function: %s\n", __func__);
    thrdpool_t *pool = (thrdpool_t *)arg;
    thrdpool_post(pool, func1, NULL);
    thrdpool_post(pool, func2, NULL);
    return NULL;
}

int main() {
    printf("Hello world\n");

    thrdpool_t *pool = thrdpool_create(4);
#if 1
    for (int i = 0; i < 1; i++) {
        pthread_t prodect_th;
        pthread_create(&prodect_th, NULL, producer, pool);
        pthread_detach(prodect_th);
    }
#else
    thrdpool_post(pool, func1, NULL);
    thrdpool_post(pool, func2, NULL);
#endif

    while (!done1 && !done2) {
        printf("done1: %d, done2: %d\n", done1, done2);
        usleep(100000);
    }

    thrdpool_terminate(pool);
    thrdpool_waitdone(pool);

    return 0;
}
#endif

#endif
